package com.qdu.spring.blog.handler;

import com.qdu.spring.blog.util.AjaxResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

@ControllerAdvice
public class GlobalDefaultExceptionHandler {

    private static Logger log = LoggerFactory.getLogger(GlobalDefaultExceptionHandler.class);

    //对Exception异常进行处理，处理结果打印到日志
    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public AjaxResult jsonErrorHandler(HttpServletRequest req,Exception e) throws Exception
    {
        log.error("请求异常:" + e.getMessage() + "   url:" + req.getRequestURL().toString());
        return AjaxResult.errorInstance("请求异常");
    }
}
